package com.zjsru.plan2023.oneday;

/**
 * @Author: CookLee
 * @Date: 2023/7/24
 * 3 的幂
 * /
 * 输入：n = 27
 * 输出：true
 */
public class IsPowerOfThree {
    
    /**
     * 没个因子都是3
     *
     * @param n n
     * @return boolean
     */
    public boolean isPowerOfThree(int n) {
        while (n != 0 && n % 3 == 0) {
            n = n / 3;
        }
        return n == 1;
    }
    
    /**
     * 枚举值
     *
     * @param n n
     * @return boolean
     */
    public boolean isPowerOfThree2(int n) {
        //枚举3的31次方内的值
        for (int i = 0; i <= 31; i++) {
            //转化为long防止溢出
            if ((long) Math.pow(3, i) == n) {
                return true;
            }
        }
        return false;
    }
    
    public static void main(String[] args) {
        IsPowerOfThree isPowerOfThree = new IsPowerOfThree();
        int n = 27;
        System.out.println(isPowerOfThree.isPowerOfThree2(n));
    }
}
